package com.zust.ysc012.dao;


import com.zust.ysc012.entity.Project;
import com.zust.ysc012.entity.Teacher;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.zust.ysc012.utility.DBUtil.*;

public class TeacherDao {
    public static String select_password_teacher_sql(String teacher_number) throws SQLException {
        String sql = "SELECT password\n" +
                "FROM teacher\n" +
                "WHERE teacher_number = ?";
        ResultSet rs;
        String password = null;
        Object[] params = new Object[1];
        params[0] = teacher_number;
        rs = executeSelect(sql, params);
        while (rs.next()) {
            password = rs.getString(1);
        }
        pre.close();
        con.close();
        return password;
    }


//    public static List<Project> select_all_project_sql(String teacher_number) throws SQLException {
//        String sql = "SELECT *\n" +
//                "FROM project\n" +
//                "WHERE teacher_number = ?";
//        ResultSet rs;
//        List<Project> projects = new ArrayList<>();
////        Project project = new Project();
//        Object[] params = new Object[1];
//        params[1] = teacher_number;
//        rs = executeSelect(sql, params);
//        while (rs.next()) {
//            Project project = new Project();
//            project.setProject_number(rs.getInt(1));
//            project.setTeacher_number(rs.getString(2));
//            project.setName(rs.getString(3));
//            project.setWin(rs.getInt(4));
//            project.setStarting_time(rs.getDate(5));
//            project.setDeadline(rs.getDate(6));
//            projects.add(project);
//        }
//        pre.close();
//        con.close();
//        return projects;
//    }

//    public static void alert_status1_sql(String project_number) throws SQLException {
//        String sql = "UPDATE teacher_project \n" +
//                "SET status = 1\n" +
//                "WHERE project_number = ?";
//        Object[] params = new Object[1];
//        params[0] = project_number;
//        executeUpdate(sql, params);
//    }
//
//    public static void alert_status2_sql(String project_number) throws SQLException {
//        String sql = "UPDATE teacher_project \n" +
//                "SET status = 2\n" +
//                "WHERE project_number = ?";
//        Object[] params = new Object[1];
//        params[0] = project_number;
//        executeUpdate(sql, params);
//    }

    public static void insert_teacher_account_sql(String account, String password) throws SQLException {
        String sql = "INSERT INTO teacher (teacher_number, `password`)\n" +
                "VALUES (?, ?)";
        Object[] params = new Object[2];
        params[0] = account;
        params[1] = password;
        executeUpdate(sql, params);
    }

    public static String select_teacher_name_sql(String teacher_number) throws SQLException {
        String sql = "SELECT `name`\n" +
                "FROM school\n" +
                "WHERE sign = 2\n" +
                "  AND number = ?";
        ResultSet rs;
        String name = null;
        Object[] params = new Object[1];
        params[0] = teacher_number;
        rs = executeSelect(sql, params);
        while (rs.next()) {
            name = rs.getString(1);
        }
        pre.close();
        con.close();
        return name;
    }

    public static int select_project_id_sql() throws SQLException {
        String sql = "SELECT project_number\n" +
                "FROM project\n" +
                "ORDER BY project_number DESC\n" +
                "LIMIT 1;";
        ResultSet rs;
        int i = 0;
        String password = null;
        Object[] params = new Object[0];
        rs = executeSelect(sql, params);
        while (rs.next()) {
            i = rs.getInt(1);
        }
        pre.close();
        con.close();
        return i;
    }

    public static void insert_project_sql(String teacher_number, String name, Date starting_time,Date deadline) {
        String sql = "INSERT INTO project(teacher_number, name, win, starting_time, deadline)\n" +
                "VALUES(?,?,?,?,?)";
        Object[] params = new Object[5];
        params[0] = teacher_number;
        params[1] = name;
        params[2] = 0;
        params[3] = starting_time;
        params[4] = deadline;
        executeUpdate(sql, params);
    }
}
